package com.cpi.cornpp.purchaseorder.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchaseorder.converter.PurchaseOrderCostVersionConverter;
import com.cpi.cornpp.purchaseorder.dao.PurchaseOrderCostVersionDAO;
import com.cpi.cornpp.purchaseorder.dto.PurchaseOrderCostVersionDTO;
import com.cpi.cornpp.purchaseorder.view.PurchaseOrderCostVersionView;

public class PurchaseOrderCostVersionService {
	private static final String ERROR_CHARGE_EXISTS = "error.message.purchaseprogram.purchaseorder.charge.exists";
	
	public void deletePurchaseOrderCostVersion(PurchaseOrderCostVersionView purchaseOrderChargeVersionView) throws PurchaseProgramException{
		PurchaseOrderCostVersionDAO purchaseOrderCostVersionDAO = null;
		PurchaseOrderCostVersionDTO purchaseOrderCostVersionDTO = null;
		try{
			purchaseOrderCostVersionDAO = new PurchaseOrderCostVersionDAO();
			purchaseOrderCostVersionDTO = PurchaseOrderCostVersionConverter.convert(purchaseOrderChargeVersionView);
			purchaseOrderCostVersionDAO.delete(purchaseOrderCostVersionDTO);
		} catch (PurchaseProgramException purchaseProgramException) {
			purchaseProgramException.printStackTrace(System.err);
			purchaseProgramException= new PurchaseProgramException(purchaseProgramException, CornPPException.LAYER_SERVICE,CornPPException.ACTION_DELETE);
			throw purchaseProgramException;
		}catch (Exception exception){
			PurchaseProgramException purchaseProgramException = null;
			exception.printStackTrace(System.err);
			purchaseProgramException = new PurchaseProgramException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_DELETE);
			throw purchaseProgramException;
		}
	}
	
	public int addPurchaseOrderCostVersion(PurchaseOrderCostVersionView pcvView) throws PurchaseProgramException{
		int insertedRows = -1;
		PurchaseOrderCostVersionDTO pcvDTO = null;
		PurchaseOrderCostVersionDAO pcvDAO = null;
		try{
			if (pcvView != null){
				pcvDAO = new PurchaseOrderCostVersionDAO();
				pcvDTO = PurchaseOrderCostVersionConverter.convert(pcvView);
				if(!pcvDAO.existsCharge(pcvDTO)){
					insertedRows = pcvDAO.insertPurchaseOrderCostVersion(pcvDTO);
				}else{
					throw new PurchaseProgramException(ERROR_CHARGE_EXISTS, CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
				}
			}	
		} catch (PurchaseProgramException ppe) {
			throw ppe;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
			throw ppe;
		}
		return insertedRows;
	}
	
	public int addPurchaseOrderCostVersions(List pcvList) throws PurchaseProgramException{
		int insertedRow = -1;
		int insertedRows = -1;
		
		if (CollectionUtils.isNotEmpty(pcvList)){
			
			for (Iterator itr = pcvList.iterator(); itr.hasNext(); ){
				insertedRow = addPurchaseOrderCostVersion((PurchaseOrderCostVersionView) itr.next());
				
				insertedRows += (insertedRow == 1) ? 1 : 0;
			}
		}
		
		return insertedRows;
	}
	
	public int updatePurchaseOrderCostVersion(PurchaseOrderCostVersionView pcvView) throws PurchaseProgramException{
		int updatedPcvs = -1;
		PurchaseOrderCostVersionDTO pcvDTO = null;
		PurchaseOrderCostVersionDAO pcvDAO = null;
		
		try{
			
			if (validateData(pcvView)){
				pcvDAO = new PurchaseOrderCostVersionDAO();
				pcvDTO = PurchaseOrderCostVersionConverter.convert(pcvView);
				updatedPcvs = pcvDAO.updatePurchaseOrderCostVersionById(pcvDTO);
			}
			
		} catch (PurchaseProgramException ppe) {
			throw ppe;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
			throw ppe;
		}
		
		return updatedPcvs;
	}
	
	public int deletePurchaseOrderCostVersionsbyPovId(Integer povId) throws PurchaseProgramException{
		int deletedRows = -1;
		PurchaseOrderCostVersionDAO pcvDAO = null;
		
		try{
			
			if (povId != null){
				pcvDAO = new PurchaseOrderCostVersionDAO();
				
				deletedRows = pcvDAO.deletePurchaseOrderCostVersionByPovId(povId);
			}
			
		} catch (PurchaseProgramException ppe) {
			throw ppe;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_DELETE);
			throw ppe;
		}
		
		return deletedRows;
	}
	
	public List getPurchaseOrderCostVersionByPovId(Integer povId) throws PurchaseProgramException{
		PurchaseOrderCostVersionDAO pcvDAO = null;
		List pcvViewList = null;
		List pcvDTOList = null;
		
		pcvDTOList = new ArrayList(0);
		
		try{
			
			if (povId != null){
				pcvDAO = new PurchaseOrderCostVersionDAO();
				
				pcvDTOList = pcvDAO.getPurchaseOrderCostsVersionsByPovId(povId);
				pcvViewList = PurchaseOrderCostVersionConverter.convertDTOList(pcvDTOList);
			}
			
		} catch (PurchaseProgramException ppe) {
			throw ppe;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_DELETE);
			throw ppe;
		}
		
		return pcvViewList;
	}
	
	private boolean validateData(PurchaseOrderCostVersionView pcvView){
		boolean validData = false;
		
		if ((pcvView != null) && (pcvView.getPurchaseOrderVersionId() != null) && (pcvView.getCostElementId() != null) && (pcvView.getAmount() != null)){
			validData = true;
		}
		
		return validData;
	}
}